/**
 * base64字符串转成文件
 * @param {String} base64Str // 允许包含前缀
 * @param {String} fileName // 文件名称：1663061363470.xlsx
 * @param {Object} callback  // 返回本地路径径URL，file:///xxx/doc/1663062980631.pdf
 */
export function base64ToFile(base64Str, fileName, callback) {
	// 去除base64前缀

	var index = base64Str.indexOf(',')
	var base64Str = base64Str.slice(index + 1, base64Str.length)
	plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {
		fs.root.getFile(fileName, {
			create: true
		}, function(entry) {
			// 获得本地路径URL，file:///xxx/doc/1663062980631.pdf
			var fullPath = entry.fullPath;

			let platform = uni.getSystemInfoSync().platform
			if (platform == 'android') {
				var Base64 = plus.android.importClass("android.util.Base64");
				var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");
				try {
					// console.log("base64Str: ", base64Str);
					var out = new FileOutputStream(fullPath);
					// 此处Base64.decode有长度限制，如果不能满足需求，可以考虑换成官方原生插件市场的【Base64转文件】
					var len = 500 * 1024; // * 1024; 这里调整每次解码的文本大小
					var txtLen = base64Str.length;
					var pages = Math.ceil(txtLen / len);
					for (var i = 0; i < pages; i++) {
						var lastPos = (i + 1) * len;
						if (lastPos > txtLen) {
							lastPos = txtLen;
						}
						var temp = base64Str.substring(i * len, lastPos);
						var respBytes = Base64.decode(temp, Base64.DEFAULT);
						out.write(respBytes);
					}
					out.close();
					// 回调  
					callback && callback(entry.toLocalURL());
				} catch (e) {
					console.log(e.message);
				}
			} else if (platform == 'ios') {
				var NSData = plus.ios.importClass('NSData');
				var nsData = new NSData();
				nsData = nsData.initWithBase64EncodedStringoptions(base64Str, 0);
				if (nsData) {
					nsData.plusCallMethod({
						writeToFile: fullPath,
						atomically: true
					});
					plus.ios.deleteObject(nsData);
				}
				// 回调  
				callback && callback(entry.toLocalURL());
			}
		})
	})
}

export function html2canvaPdf(html2Canvas, JsPDF, ownerVm, selector, callback) {
	html2Canvas(selector, {
		allowTaint: false, //允许污染
		taintTest: true, //在渲染前测试图片(没整明白有啥用)
		useCORS: true, //使用跨域(当allowTaint为true时这段代码没什么用,下面解释)
	}).then(function(canvas) {
		let contentWidth = canvas.width
		let contentHeight = canvas.height
		//一页pdf显示html页面生成的canvas高度;
		let pageHeight = contentWidth / 592.28 * 841.89
		//生成pdf的html页面高度
		let leftHeight = contentHeight
		//页面偏移
		let position = 0
		//a4纸的尺寸[595.28,841.89]，html页面生成的canvas在pdf中图片的宽高
		let imgWidth = 595.28
		let imgHeight = 592.28 / contentWidth * contentHeight
		// canvas.crossOrigin="anonymous";
		let pageData = canvas.toDataURL('image/jpeg', 1.0);
		let PDF = new JsPDF('', 'pt', 'a4')
		//有两个高度需要区分，一个是html页面的实际高度，和生成pdf的页面高度(841.89)
		//当内容未超过pdf一页显示的范围，无需分页
		if (leftHeight < pageHeight) {
			PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
		} else {
			while (leftHeight > 0) {
				PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
				leftHeight -= pageHeight
				position -= 841.89
				if (leftHeight > 0) {
					//避免添加空白页
					PDF.addPage()
				}
			}
		}
		plus.android.requestPermissions(['android.permission.WRITE_EXTERNAL_STORAGE'], function(e) {
			if (e.deniedAlways.length > 0) { //权限被永久拒绝
				// 弹出提示框解释为何需要读写手机储存权限，引导用户打开设置页面开启
				uni.showModal({
					title: '存储权限',
					content: '您拒绝了存储权限，请去设置-应用开启存储权限。',
					success: function(res) {
						if (res.confirm) {
							// console.log('用户点击确定');
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			}
			if (e.deniedPresent.length > 0) { //权限被临时拒绝
				// 弹出提示框解释为何需要读写手机储存权限，可再次调用plus.android.requestPermissions申请权限
				plus.android.requestPermissions(['android.permission.WRITE_EXTERNAL_STORAGE'])
			}
			if (e.granted.length > 0) { //权限被允许
				//调用依赖获取读写手机储存权限的代码
				let base64Str = PDF.output('dataurlstring');
				if (callback) {
					callback(base64Str)
				} else {
					ownerVm.callMethod('savePDF', base64Str)
				}
			}
		}, function(e) {});
	})
}

export function savePDF(base64) {
	uni.showLoading({
		title: '导出中'
	});
	var fileName = this.fileName;
	base64ToFile(base64, fileName, function(path) {
		console.log('result', path);
		uni.hideLoading()
		uni.showModal({
			title: '导出成功',
			content: '文件地址:' + path +
				'\n 是否打开？',
			success: function(res) {
				if (res.confirm) {
					plus.runtime.openFile(path); //用第三方程序打开文件
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	})
}